'use strict';

app.controller('orderFormCtrl', function ($rootScope, $scope, $http) {
    // console.log("bbbbbbbbbb");
    $http.get("/api/getOrders").then(function(response){
        if(response.status==200){
            console.log(response.data);
        }
    })
    // 订单数组
    $scope.food_forms = [
    	{
    		"order_number":"123161645",
    		"number":"01",
    		"money":123,
    		"time":"2017-01-12",
            "user_id":"",
            "remarks":"微辣",
            "food_detail":[
                {
                    "id":"1",
                    "num":"1"
                },
                {
                    "id":"2",
                    "num":"3"
                },
                {
                    "id":"9",
                    "num":"1"
                }
            ]
    	},
    	{
    		"order_number":"156155545",
    		"number":"02",
    		"money":121,
    		"time":"2017-01-19",
            "user_id":"",
            "remarks":"不要辣椒",
            "food_detail":[
                {
                    "id":"3",
                    "num":"3"
                },
                {
                    "id":"2",
                    "num":"1"
                }
            ]
    	},
    	{
    		"order_number":"54564646",
    		"number":"56",
    		"money":78,
    		"time":"2017-04-12",
            "user_id":"",
            "remarks":"特辣",
            "food_detail":[
                {
                    "id":"6",
                    "num":"1"
                }
            ]
    	},
    	{
    		"order_number":"4566514564",
    		"number":"131",
    		"money":56,
    		"time":"2017-02-12",
            "user_id":"",
            "remarks":"微微辣",
            "food_detail":[
                {
                    "id":"7",
                    "num":"2"
                },
                {
                    "id":"2",
                    "num":"1"
                }
            ]
    	},
    	{
    		"order_number":"56644664",
    		"number":"09",
    		"money":231,
    		"time":"2017-01-24",
            "user_id":"",
            "remarks":"微微辣",
            "food_detail":[
                {
                    "id":"9",
                    "num":"2"
                },
                {
                    "id":"1",
                    "num":"1"
                },
                {
                    "id":"2",
                    "num":"2"
                }
            ]
    	},
    	{
    		"order_number":"238794264",
    		"number":"11",
    		"money":64,
    		"time":"2017-08-09",
            "user_id":"",
            "remarks":"不要葱花",
            "food_detail":[
                {
                    "id":"2",
                    "num":"1"
                },
                {
                    "id":"8",
                    "num":"1"
                },
                {
                    "id":"5",
                    "num":"1"
                },
                {
                    "id":"4",
                    "num":"1"
                }
            ]
    	},
    	{
    		"order_number":"3791255125",
    		"number":"15",
    		"money":97,
    		"time":"2017-09-01",
            "user_id":"",
            "remarks":"不要辣椒",
            "food_detail":[
                {
                    "id":"5",
                    "num":"1"
                },
                {
                    "id":"0",
                    "num":"1"
                }
            ]
    	},
    	{
    		"order_number":"39758135",
    		"number":"16",
    		"money":85,
    		"time":"2017-03-05",
            "user_id":"",
            "remarks":"不要酱油",
            "food_detail":[
                {
                    "id":"0",
                    "num":"2"
                }
            ]
    	},
    	{
    		"order_number":"456746921",
    		"number":"13",
    		"money":211,
    		"time":"2017-03-06",
            "user_id":"",
            "remarks":"不要香菜",
            "food_detail":[
                {
                    "id":"2",
                    "num":"1"
                },
                {
                    "id":"4",
                    "num":"1"
                },
                {
                    "id":"7",
                    "num":"1"
                }
            ]
    	},
    	{
    		"order_number":"5687954622",
    		"number":"14",
    		"money":57,
    		"time":"2017-01-01",
            "user_id":"",
            "remarks":"不要香菜",
            "food_detail":[
                {
                    "id":"9",
                    "num":"1"
                },
                {
                    "id":"0",
                    "num":"1"
                }
            ]
    	}
        ,
        {
            "order_number":"5687954622",
            "number":"14",
            "money":57,
            "time":"2017-01-01",
            "user_id":"",
            "remarks":"不要香菜",
            "food_detail":[
                {
                    "id":"9",
                    "num":"1"
                },
                {
                    "id":"0",
                    "num":"1"
                }
            ]
        }
        ,
        {
            "order_number":"5687954622",
            "number":"14",
            "money":57,
            "time":"2017-01-01",
            "user_id":"",
            "remarks":"不要香菜",
            "food_detail":[
                {
                    "id":"9",
                    "num":"1"
                },
                {
                    "id":"0",
                    "num":"1"
                }
            ]
        }
        ,
        {
            "order_number":"5687954622",
            "number":"14",
            "money":57,
            "time":"2017-01-01",
            "user_id":"",
            "remarks":"不要香菜",
            "food_detail":[
                {
                    "id":"9",
                    "num":"1"
                },
                {
                    "id":"0",
                    "num":"1"
                }
            ]
        }
        ,
        {
            "order_number":"5687954622",
            "number":"14",
            "money":57,
            "time":"2017-01-01",
            "user_id":"",
            "remarks":"不要香菜",
            "food_detail":[
                {
                    "id":"9",
                    "num":"1"
                },
                {
                    "id":"0",
                    "num":"1"
                }
            ]
        }
        ,
        {
            "order_number":"5687954622",
            "number":"14",
            "money":57,
            "time":"2017-01-01",
            "user_id":"",
            "remarks":"不要香菜",
            "food_detail":[
                {
                    "id":"9",
                    "num":"1"
                },
                {
                    "id":"0",
                    "num":"1"
                }
            ]
        }
    ];

    // 食物
    $scope.food_menu = [
        {
            "id":"0",
            "name":"叫化鸡",
            "price":7.79,
            "detail":"pellentesque enim. Aliquam tempor",
            "img":"assets/images/img-1.jpg",
            "type":"business"
        },
        {
            "id":"1",
            "name":"茄汁鱼卷",
            "price":7.79,
            "detail":"pellentesque enim. Aliquam tempor",
            "img":"assets/images/img-2.jpg",
            "type":"teatime"
        },
        {
            "id":"2",
            "name":"鱼香肉丝",
            "price":7.79,
            "detail":"pellentesque enim. Aliquam tempor",
            "img":"assets/images/img-3.jpg",
            "type":"westernfood"
        },
        {
            "id":"3",
            "name":"干煸冬笋",
            "price":7.79,
            "detail":"pellentesque enim. Aliquam tempor",
            "img":"assets/images/img-4.jpg",
            "type":"drink"
        },
        {
            "id":"4",
            "name":"魔芋烧鸭",
            "price":7.79,
            "detail":"pellentesque enim. Aliquam tempor",
            "img":"assets/images/img-5.jpg",
            "type":"teatime"
        },
        {
            "id":"5",
            "name":"锅贴鱼片",
            "price":7.79,
            "detail":"pellentesque enim. Aliquam tempor",
            "img":"assets/images/img-6.jpg",
            "type":"westernfood"
        },
        {
            "id":"6",
            "name":"汉堡包",
            "price":7.79,
            "detail":"pellentesque enim. Aliquam tempor",
            "img":"assets/images/img-7.jpg",
            "type":"coffee"
        },
        {
            "id":"7",
            "name":"麻辣肉丁",
            "price":7.79,
            "detail":"pellentesque enim. Aliquam tempor",
            "img":"assets/images/img-8.jpg",
            "type":"business"
        },
        {
            "id":"8",
            "name":"鱼香茄饼",
            "price":7.79,
            "detail":"pellentesque enim. Aliquam tempor",
            "img":"assets/images/img-9.jpg",
            "type":"coffee"
        },
        {
            "id":"9",
            "name":"冬菜肉末",
            "price":7.79,
            "detail":"pellentesque enim. Aliquam tempor",
            "img":"assets/images/img-10.jpg",
            "type":"tea"
        },

    ];


    // 订单详情数组
    $scope.forms_detail = [];
    // $scope.food_detail = [];

    // 查询订单详情
    $scope.search_detail = function(index){  
        $scope.forms_detail = $scope.food_forms[index];
        console.log($scope.forms_detail);
        var food_detail = $scope.food_forms[index].food_detail;
        for (var i = 0; i < food_detail.length; i++) {
            // debugger;
            for (var j = 0; j < $scope.food_menu.length; j++) {
                // debugger;
                if (food_detail[i].id == $scope.food_menu[j].id) {
                    food_detail[i].name = $scope.food_menu[j].name;
                    food_detail[i].price = $scope.food_menu[j].price;
                    food_detail[i].img = $scope.food_menu[j].img;
                }
            }
        }
        console.log(food_detail);
        $scope.forms_detail.food_detail = food_detail;
        console.log($scope.forms_detail);
        return $scope.forms_detail;
    };


    //计算总价 
    $scope.allSum=function(){  
        var allPrice = 0;  
        for(var i= 0;i<$scope.food_detail.length;i++){  
            allPrice+=$scope.food_detail[i].price*$scope.food_detail[i].num;  
            // console.log(allPrice);
        }  
        return allPrice;  
    };

    

    var LODOP; //声明为全局变量
    $scope.printPreview = function(index){  
        //创建小票打印页  
        LODOP = $scope.CreatePrintPage(index);  
        //打印预览  
        LODOP.PREVIEW();      
    };
    $scope.printOrder = function(index) {  
          
        //创建小票打印页  
        LODOP = $scope.CreatePrintPage(index);  
        //开始打印  
        LODOP.PRINT(); 
        //打印预览  
        //LODOP.PREVIEW();   
      
    };
    //创建小票打印页
    $scope.CreatePrintPage =function(index) {
        var json = $scope.search_detail(index);
        console.log(json); 
        // 当前时间
        var Time;
        var today = new Date();
        Time = today.toLocaleString();

        var hPos=10,//小票上边距  
        pageWidth=580,//小票宽度  
        rowHeight=15,//小票行距  
        //获取控件对象  
        LODOP=getLodop(document.getElementById('LODOP_OB'),document.getElementById('LODOP_EM'));   
        //初始化   
        // LODOP.PRINT_INIT("打印控件功能演示_Lodop功能_名片"); 
        LODOP.PRINT_INIT(""); 
        //添加小票标题文本  
        LODOP.ADD_PRINT_TEXT(hPos,30,pageWidth,rowHeight,$rootScope.shop_set.name);  
        //上边距往下移  
        hPos+=rowHeight;  
          
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"订单编号:");  
        LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.order_number);  
        hPos+=rowHeight; //换行  
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"下单时间:");  
        LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,Time);  
        hPos+=rowHeight;  
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"餐号:");  
        LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.number);  
        hPos+=rowHeight;  
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"备注:");  
        LODOP.ADD_PRINT_TEXT(hPos,40,pageWidth,rowHeight,json.remarks);  
        hPos+=rowHeight;  
        // LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"取货门店:");  
        // LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.shop);  
        // hPos+=rowHeight;  
        LODOP.ADD_PRINT_LINE(hPos,2, hPos, pageWidth,2, 1);  
        hPos+=5;  
        LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,"商品名称");  
        LODOP.ADD_PRINT_TEXT(hPos,70,pageWidth,rowHeight,"单价");  
        LODOP.ADD_PRINT_TEXT(hPos,110,pageWidth,rowHeight,"数量");  
        LODOP.ADD_PRINT_TEXT(hPos,140,pageWidth,rowHeight,"小计");  
        hPos+=rowHeight;  
        //遍历json的商品数组  
        for(var i=0;i<json.food_detail.length;i++){  
              
            if(json.food_detail[i].name.length<4){  
                LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,json.food_detail[i].name);  
            }else {  
                //商品名字过长,其他字段需要换行  
                LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,json.food_detail[i].name);  
                hPos+=rowHeight;  
            }  
            LODOP.ADD_PRINT_TEXT(hPos,70,pageWidth,rowHeight,json.food_detail[i].price);  
            LODOP.ADD_PRINT_TEXT(hPos,115,pageWidth,rowHeight,json.food_detail[i].num);  
            LODOP.ADD_PRINT_TEXT(hPos,140,pageWidth,rowHeight,json.food_detail[i].price*json.food_detail[i].num);  
            hPos+=rowHeight;  
        }  
        //商品遍历打印完毕,空一行  
        hPos+=rowHeight;  
        //合计  
        // LODOP.ADD_PRINT_TEXT(hPos,80,pageWidth,rowHeight,"合计:"+json.totalCount);
        LODOP.ADD_PRINT_TEXT(hPos,80,pageWidth,rowHeight,"合计:");  
        LODOP.ADD_PRINT_TEXT(hPos,130,pageWidth,rowHeight,"￥"+json.money);  
          
        hPos+=rowHeight;  
        // LODOP.ADD_PRINT_TEXT(hPos,2,pageWidth,rowHeight,(new Date()).toLocaleDateString()+" "+(new Date()).toLocaleTimeString())  
        hPos+=rowHeight;  
        LODOP.ADD_PRINT_TEXT(hPos,25,pageWidth,rowHeight,"谢谢惠顾,欢迎下次光临!");  
        //初始化打印页的规格  
        LODOP.SET_PRINT_PAGESIZE(3,pageWidth,45,"XXXXX订单信息");  
        return LODOP; 
    }



    $(document).ready(function(){ 
        $('.tableFilter').dataTable({ 
            "oLanguage": { 
                "sLengthMenu": "每页显示 _MENU_ 条记录", 
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据", 
                "sInfoEmpty": "没有数据", 
                "sSearch" : "搜索:",//搜索框前的文本设置
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)", 
                "oPaginate": { 
                    "sFirst": "首页", 
                    "sPrevious": "前一页", 
                    "sNext": "后一页", 
                    "sLast": "尾页"
                }, 
                "sZeroRecords": "没有检索到数据", 
                "sProcessing": "<img src='./loading.gif' />"
            } 
            
        }); 

    });





  
});
